package com.ubox.mqtt_upush.utils;

import java.util.ArrayList;
import java.util.Collection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Created with IntelliJ IDEA.
 * User: zhoubo
 * Date: 13-10-23
 * Time: 上午10:45
 * To change this template use File | Settings | File Templates.
 */
public class JsonUtil {
	private static JsonUtil instance = null;
	private static ObjectMapper mapper = null;

	private final static Logger logger = LoggerFactory.getLogger(JsonUtil.class);

	private JsonUtil() {
		mapper = new ObjectMapper().setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
		mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
		mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	}

	public static ObjectMapper getInstance() {
		if (instance == null) {
			synchronized (JsonUtil.class) {
				if (instance == null) {
					instance = new JsonUtil();
				}
			}
		}
		return mapper;
	}

	/**
	 * 将java对象转换成json字符串
	 *
	 * @param obj 准备转换的对象
	 * @return json字符串
	 * @throws Exception
	 */
	public static String toString(Object obj) {
		try {
			ObjectMapper objectMapper = getInstance();
			String json = objectMapper.writeValueAsString(obj);
			return json;
		}
		catch (Exception e) {
			logger.error("JsonUtils toString error", e);
		}
		return null;
	}

	/**
	 * 将java对象转换成json字符串
	 *
	 * @param obj 准备转换的对象
	 * @return json字符串
	 * @throws Exception
	 */
	public static byte[] toByte(Object obj) {
		try {
			ObjectMapper objectMapper = getInstance();
			byte[] json = objectMapper.writeValueAsBytes(obj);
			return json;
		}
		catch (Exception e) {
			logger.error("JsonUtils toByte error", e);
		}
		return null;
	}

	/**
	 * 将json字符串转换成java对象
	 * @param <T>
	 *
	 * @param json 准备转换的json字符串
	 * @param cls  准备转换的类
	 * @return
	 * @throws Exception
	 */
	public static <T> T toBean(byte[] json, Class<T> cls) {
		try {
			ObjectMapper objectMapper = getInstance();
			T vo = objectMapper.readValue(json, cls);
			return vo;
		}
		catch (Exception e) {
			logger.error("JsonUtils byte[] toBean error", e);
		}
		return null;
	}

	/**
	 * 将json字符串转换成java对象
	 * @param <T>
	 *
	 * @param json 准备转换的json字符串
	 * @param cls  准备转换的类
	 * @return
	 * @throws Exception
	 */
	public static <T> T toBean(String json, Class<T> cls) {
		try {
			ObjectMapper objectMapper = getInstance();
			T vo = objectMapper.readValue(json, cls);
			return vo;
		}
		catch (Exception e) {
			logger.error("JsonUtils String toBean error Json {}",json, e);
		}
		return null;
	}

	/**
     * 将json字符串转换成List java对象
     * @param <T>
     * @param json 准备转换的json字符串
     * @param cls  准备转换的类
     * @return
     * @throws Exception
     */
    public static <T> T toList(String json, Class<?> cls)  {
        try {
        	 getInstance();
        	JavaType javaType = getCollectionType(ArrayList.class, cls); 
            return mapper.readValue(json, javaType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**   
     * 获取泛型的Collection Type  
     * @param collectionClass 泛型的Collection   
     * @param elementClasses 元素类   
     * @return JavaType Java类型   
     * @since 1.0   
     */   
 public static JavaType getCollectionType(Class<? extends Collection> collectionClass, Class<?> elementClasses) {   
     return mapper.getTypeFactory().constructCollectionType(collectionClass, elementClasses);   
 }
}
